<template>
    <div ref="boardMain" class="board-main">
        <div class="title">基礎數據</div>
        <div class="card-list">
            <div class="card-item" v-for="item in count" :key="item.name">
                <div class="card-item-title">{{ item.name }}</div>
                <div class="card-item-value">{{ item.count }}</div>
            </div>
        </div>
        <div class="line-chart">
            <div class="title">平臺用戶趨勢圖</div>
            <div class="line-chart-1" ref="lineChart1"></div>
            <div class="title">車輛歸檔趨勢圖</div>
            <div class="line-chart-2" ref="lineChart2"></div>
        </div>
    </div>
</template>

<script>
import * as echarts from 'echarts';
import axios from "~/plugins/axios.js";

export default {
    name: 'adminIndexPage',
    layout: 'admin',
    ssr: false,
    pageTitle: '儀表盤',
    data() {
        return {
            count: [],
            lineChart1: {},
            lineChart2: {}
        }
    },
    methods: {
        async getData() {
            const loading = this.$loading({
                lock: true,
                text: '数据加载中',
                target: this.$refs.boardMain
            });
            const countRes = await axios.get('/admin/system.Index/count', this.form);
            if (countRes.code == 200) {
                this.count = countRes.data.count;
            }
            const char1Res = await axios.get('/admin/system.Index/member', this.form);
            if (char1Res.code == 200) {
                this.lineChart1 = char1Res.data.number;
            }
            const char2Res = await axios.get('/admin/system.Index/carfile', this.form).finally(() => {
                loading.close();
            });
            if (char2Res.code == 200) {
                this.lineChart2 = char2Res.data.number;
            }
            this.initChart1();
            this.initChart2();
        },

        initChart1() {
            const myChart = echarts.init(this.$refs.lineChart1);
            const options = {
                title: {
                    text: '',
                },
                xAxis: {
                    type: 'category',
                    data: this.lineChart1.xAxis,
                },
                yAxis: {
                    type: 'value',
                },
                series: this.lineChart1.series,
            };
            myChart.setOption(options);
        },

        initChart2() {
            const myChart = echarts.init(this.$refs.lineChart2);
            const options = {
                title: {
                    text: '',
                },
                xAxis: {
                    type: 'category',
                    data: this.lineChart2.xAxis,
                },
                yAxis: {
                    type: 'value',
                },
                series: this.lineChart2.series,
            };
            myChart.setOption(options);
        }
    },
    mounted() {
        this.getData();
    }
}
</script>

<style scoped lang="less">
.board-main {
    .title {
        margin-bottom: 20px;
        font-size: 16px;

        &::before {
            display: inline-block;
            margin-right: 10px;
            content: '';
            width: 8px;
            height: 16px;
            background-color: rgba(62, 39, 35, 1);
            border-radius: 7px;
            vertical-align: middle;
        }
    }

    .card-list {
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
        margin-bottom: 40px;

        .card-item {
            width: calc(20% - 20px);
            margin-right: 20px;
            text-align: center;
            height: 121px;
            line-height: 20px;
            border-radius: 5px;
            background: linear-gradient(180deg, rgba(62, 39, 35, 1) 0%, rgba(141, 110, 99, 1) 100%);
            font-size: 14px;
            box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.4);
            display: flex;
            align-items: center;
            justify-content: center;
            flex-wrap: wrap;
            flex-direction: column;
            color: #FFFFFF;
            font-weight: bold;
            font-size: 20px;

            .card-item-title {
                width: 100%;
            }

            .card-item-value {
                width: 100%;
                margin-top: 20px;
            }

            &:last-child {
                margin-right: 0;
            }
        }
    }

    .line-chart {
        .line-chart-1 {
            height: 400px;
            margin-bottom: 40px;
        }

        .line-chart-2 {
            height: 400px;
        }
    }
}
</style>